/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
    
        vector<vector<int>> ret;
        if(root==nullptr)
            return ret;
        int flg=0;
        queue<TreeNode*> q;
        q.push(root);
        while(q.size())
        {
            vector<int> tmp;
            int sz=q.size();
            while(sz--)
            {
                TreeNode* rt=q.front();
                q.pop();
                tmp.push_back(rt->val);
                if(rt->left)
                {
                    q.push(rt->left);
                }
                if(rt->right)
                {
                    q.push(rt->right);
                }
            }
            if(flg)
            {
                
                reverse(tmp.begin(),tmp.end());
            }
            flg=!flg;
            ret.push_back(tmp);

        }
        return ret;
    }
};
